#!/bin/sh

# Slackware build script for nicotine+
# Written by Iskar Enev (iskar.enev@gmail.com)

# Modified by the SlackBuilds.org project

PRGNAM=nicotine+
NAME=nicotine
VERSION=1.2.8
ARCH=${ARCH:-i486}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
CWD=$(pwd)
TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# Change to NO if you don't want the trayicon shared object to be built
# - or call the build script with TRAYICON=NO ./nicotine-plus.SlackBuild 
TRAYICON=${TRAYICON:-YES}

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP || exit 1
rm -rf $PRGNAM-$VERSION
tar -xjvf $CWD/$PRGNAM-$VERSION.tar.bz2 || exit 1
cd $PRGNAM-$VERSION || exit 1
chown -R root:root .
chmod -R u+w,go+r-w,a-s .

# Tray icon has to be compiled first
if [ "$TRAYICON" = "YES" ]; then
  ( cd trayicon
    ./autogen.py || exit 1
    make || exit 1
    make install DESTDIR=$PKG || exit 1
  ) || exit 1
fi

python setup.py bdist || exit 1

tar xzvf dist/$NAME-$VERSION.linux-i686.tar.gz -C $PKG || exit 1

( cd $PKG
  find . | xargs file | grep "executable" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
  find . | xargs file | grep "shared object" | grep ELF | cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null
)

mv $PKG/usr/share/man $PKG/usr
( cd $PKG/usr/man
  find . -type f -exec gzip -9 {} \;
  for i in $(find . -type l) ; do ln -s $(readlink $i).gz $i.gz ; rm $i ; done
)

# Fix icon path in desktop file so that it will show in kde
# Also, we'll use the larger icon to ensure proper scaling
sed -i 's%Icon=nicotine-plus-32px.png%Icon=/usr/share/pixmaps/nicotine/nicotine-plus-96px.png%' \
  $PKG/usr/share/applications/nicotine.desktop || exit 1

# Let's put these icons and such in a subdirectory of their own instead of
# spamming /usr/share/pixmaps with all sorts of stuff
rm $PKG/usr/share/pixmaps/*
mkdir -p $PKG/usr/share/pixmaps/nicotine
cp -a files/*.png img/*.png img/*.ico img/*.xcf img/README \
  $PKG/usr/share/pixmaps/nicotine

mkdir -p $PKG/usr/doc
cp -a COPYING MANIFEST PKG-INFO $PKG/usr/share/$NAME/documentation
mv $PKG/usr/share/$NAME/documentation $PKG/usr/doc/$PRGNAM-$VERSION
cat $CWD/nicotine-plus.SlackBuild > $PKG/usr/doc/$PRGNAM-$VERSION/nicotine-plus.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc
cat $CWD/doinst.sh > $PKG/install/doinst.sh

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.tgz
